<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/mes/mes/Stock.jsx或Stock.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
        :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
        :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
        :searchAfter="searchAfter" :addBefore="addBefore" :updateBefore="updateBefore" :rowClick="rowClick"
        :modelOpenBefore="modelOpenBefore" :modelOpenAfter="modelOpenAfter">
        <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
        <template #gridHeader>
        </template>
        <template #gridFooter>
            <div class="locationDetail">
                <location-detail ref="locationDetailRef"></location-detail>
            </div>
        </template>
    </view-grid>
</template>
<script setup lang="jsx">
import locationDetail from './Location.vue'
import extend from "@/extension/mes/mes/Stock.jsx";
import viewOptions from './Stock/options.js'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
const grid = ref(null);
const { proxy } = getCurrentInstance()
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())

let gridRef;//对应[表.jsx]文件中this.使用方式一样
const locationDetailRef = ref();
//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;
    //缓存主表方法，返回主表选中的行，在下面的bom明细表[MES_Bom_Detail]中会调用
    proxy.base.setItem('getStockSelectRow', () => {
        return gridRef.getTable(true).getSelected();
    })

    //设置弹出框编辑宽度
    // gridRef.boxOptions.width = 1100
    //启用明细表排序
    gridRef.detailOptions.sortable = true

    gridRef.detailOptions.pagination.sortName = "LocationId";  //明细表排序字字段
    gridRef.detailOptions.pagination.order = "asc"; //明细表排序方式desc或者asc
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
    columns.forEach(x => {
        //开启表头过滤
        x.filterData = true;
    })
    gridRef.height = gridRef.height - 310;
    if (gridRef.height < 200) {
        gridRef.height = 200;
    }
    gridRef.buttons.forEach(x => {
        if (x.name == '高级查询') {
            x.hidden = true;
        }
    })
    gridRef.buttons.splice(3, 0, {
        name: '启用仓位', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'danger',
        plain: true,
        onClick: () => {

            startLocation()
        }
    })
    gridRef.buttons.splice(4, 0, {
        name: '禁用', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'danger',
        plain: true,
        onClick: () => {
            stockDisabled()
        }
    })
    gridRef.buttons.splice(5, 0, {
        name: '反禁用', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'danger',
        plain: true,
        onClick: () => {
            unStockDisabled()
        }
    })
}
const searchBefore = async (param) => {
    //界面查询前,可以给param.wheres添加查询参数
    //返回false，则不会执行查询
    return true;
}
const searchAfter = async (rows, result) => {
    return true;
}
const addBefore = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    return true;
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}

const startLocation = (rows) => {
    var rows = gridRef.getSelectRows()

    if (rows.length == 0) {
        proxy.$message.error("请选择仓库！");
        return
    }
    if (rows[0].AuditStatus == 0 || rows[0].AuditStatus == null) {
        proxy.$message.error("该仓库未审核！");
        return
    }

    let url = 'api/Stock/startLocation';

    proxy.http.post(url, rows).then((result) => {
        proxy.$message.success("操作成功！");
        gridRef.search();
        // 刷新主表列表
    });

}
const stockDisabled = (rows) => {
    var rows = gridRef.getSelectRows()

    if (rows.length == 0) {
        proxy.$message.error("请选择仓库！");
        return
    }
    if (rows[0].AuditStatus == 0 || rows[0].AuditStatus == null) {
        proxy.$message.error("该仓库未审核！");
        return
    }

    let url = 'api/Stock/stockDisabled';

    proxy.http.post(url, rows).then((result) => {
        proxy.$message.success("仓库禁用！");
        gridRef.search();
        // 刷新主表列表
    });
}
const unStockDisabled = (rows) => {
    var rows = gridRef.getSelectRows()

    if (rows.length == 0) {
        proxy.$message.error("请选择仓库！");
        return
    }
    if (rows[0].AuditStatus == 0) {
        proxy.$message.error("该仓库未审核！");
        return
    }

    let url = 'api/Stock/unStockDisabled';

    proxy.http.post(url, rows).then((result) => {
        proxy.$message.success("仓库启用！");
        gridRef.search();
        // 刷新主表列表
    });
}

const rowClick = ({ row, column, event }) => {
    gridRef.buttons.forEach(button => {

        //如果点击行是仓位是禁用状态，且点击按钮时启用仓位
        if (row.IsLocation == 0 && button.name == "禁用仓位") {

            button.name = "启用仓位"
            button.hidden = false

        }
        if (row.IsLocation == 1 && button.name == "启用仓位") {

            button.name = "禁用仓位"
            button.hidden = false

        }

    });
    //查询界面点击行事件
    grid.value.clearSelection();
    //点击行默认选中
    grid.value.toggleRowSelection(row); //单击行时选中当前行;
    //加载明细表
    locationDetailRef.value.$refs.grid.search();
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {
    gridRef.setFormReadonly(false);

    if (gridRef.currentAction == 'Add') {
        gridRef.editFormFields.IsEnabled = 1;
        gridRef.boxButtons.forEach((btn) => {
            if (btn.name == '保存') {
                btn.hidden = false;
                //或者设置只读
                // btn.readonly = true;
            }

        });
    }
    else {
        if (row.AuditStatus != 0) {
            gridRef.setFormReadonly(true);
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = true;
                    //或者设置只读
                    // btn.readonly = true;
                }

            });
        }
        else {
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = false;
                    //或者设置只读
                    // btn.readonly = true;
                }
            });

        }
    }
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
